Solving an issue in r69542
[lhc/web/wiklou.git] / includes / AjaxResponse.php
1 <?php
2 /**
3 * @file
4 * @ingroup Ajax
5 */
6
7 if ( !defined( 'MEDIAWIKI' ) ) {
8 die( 1 );
9 }
10
11 /**
12 * Handle responses for Ajax requests (send headers, print
13 * content, that sort of thing)
14 *
15 * @ingroup Ajax
16 */
17 class AjaxResponse {
18 /** Number of seconds to get the response cached by a proxy */
19 private $mCacheDuration;
20
21 /** HTTP header Content-Type */
22 private $mContentType;
23
24 /** Disables output. Can be set by calling $AjaxResponse->disable() */
25 private $mDisabled;
26
27 /** Date for the HTTP header Last-modified */
28 private $mLastModified;
29
30 /** HTTP response code */
31 private $mResponseCode;
32
33 /** HTTP Vary header */
34 private $mVary;
35
36 /** Content of our HTTP response */
37 private $mText;
38
39 function __construct( $text = null ) {
40 $this->mCacheDuration = null;
41 $this->mVary = null;
42
43 $this->mDisabled = false;
44 $this->mText = '';
45 $this->mResponseCode = '200 OK';
46 $this->mLastModified = false;
47 $this->mContentType = 'application/x-wiki';
48
49 if ( $text ) {
50 $this->addText( $text );
51 }
52 }
53
54 function setCacheDuration( $duration ) {
55 $this->mCacheDuration = $duration;
56 }
57
58 function setVary( $vary ) {
59 $this->mVary = $vary;
60 }
61
62 function setResponseCode( $code ) {
63 $this->mResponseCode = $code;
64 }
65
66 function setContentType( $type ) {
67 $this->mContentType = $type;
68 }
69
70 function disable() {
71 $this->mDisabled = true;
72 }
73
74 /** Add content to the response */
75 function addText( $text ) {
76 if ( ! $this->mDisabled && $text ) {
77 $this->mText .= $text;
78 }
79 }
80
81 /** Output text */
82 function printText() {
83 if ( ! $this->mDisabled ) {
84 print $this->mText;
85 }
86 }
87
88 /** Construct the header and output it */
89 function sendHeaders() {
90 global $wgUseSquid, $wgUseESI;
91
92 if ( $this->mResponseCode ) {
93 $n = preg_replace( '/^ *(\d+)/', '\1', $this->mResponseCode );
94 header( "Status: " . $this->mResponseCode, true, (int)$n );
95 }
96
97 header ( "Content-Type: " . $this->mContentType );
98
99 if ( $this->mLastModified ) {
100 header ( "Last-Modified: " . $this->mLastModified );
101 } else {
102 header ( "Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . " GMT" );
103 }
104
105 if ( $this->mCacheDuration ) {
106 # If squid caches are configured, tell them to cache the response,
107 # and tell the client to always check with the squid. Otherwise,
108 # tell the client to use a cached copy, without a way to purge it.
109
110 if ( $wgUseSquid ) {
111 # Expect explicite purge of the proxy cache, but require end user agents
112 # to revalidate against the proxy on each visit.
113 # Surrogate-Control controls our Squid, Cache-Control downstream caches
114
115 if ( $wgUseESI ) {
116 header( 'Surrogate-Control: max-age=' . $this->mCacheDuration . ', content="ESI/1.0"' );
117 header( 'Cache-Control: s-maxage=0, must-revalidate, max-age=0' );
118 } else {
119 header( 'Cache-Control: s-maxage=' . $this->mCacheDuration . ', must-revalidate, max-age=0' );
120 }
121
122 } else {
123
124 # Let the client do the caching. Cache is not purged.
125 header ( "Expires: " . gmdate( "D, d M Y H:i:s", time() + $this->mCacheDuration ) . " GMT" );
126 header ( "Cache-Control: s-max-age={$this->mCacheDuration},public,max-age={$this->mCacheDuration}" );
127 }
128
129 } else {
130 # always expired, always modified
131 header ( "Expires: Mon, 26 Jul 1997 05:00:00 GMT" ); // Date in the past
132 header ( "Cache-Control: no-cache, must-revalidate" ); // HTTP/1.1
133 header ( "Pragma: no-cache" ); // HTTP/1.0
134 }
135
136 if ( $this->mVary ) {
137 header ( "Vary: " . $this->mVary );
138 }
139 }
140
141 /**
142 * checkLastModified tells the client to use the client-cached response if
143 * possible. If sucessful, the AjaxResponse is disabled so that
144 * any future call to AjaxResponse::printText() have no effect. The method
145 * returns true iff the response code was set to 304 Not Modified.
146 */
147 function checkLastModified ( $timestamp ) {
148 global $wgCachePages, $wgCacheEpoch, $wgUser;
149 $fname = 'AjaxResponse::checkLastModified';
150
151 if ( !$timestamp || $timestamp == '19700101000000' ) {
152 wfDebug( "$fname: CACHE DISABLED, NO TIMESTAMP\n" );
153 return;
154 }
155
156 if ( !$wgCachePages ) {
157 wfDebug( "$fname: CACHE DISABLED\n", false );
158 return;
159 }
160
161 if ( $wgUser->getOption( 'nocache' ) ) {
162 wfDebug( "$fname: USER DISABLED CACHE\n", false );
163 return;
164 }
165
166 $timestamp = wfTimestamp( TS_MW, $timestamp );
167 $lastmod = wfTimestamp( TS_RFC2822, max( $timestamp, $wgUser->mTouched, $wgCacheEpoch ) );
168
169 if ( !empty( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) ) {
170 # IE sends sizes after the date like this:
171 # Wed, 20 Aug 2003 06:51:19 GMT; length=5202
172 # this breaks strtotime().
173 $modsince = preg_replace( '/;.*$/', '', $_SERVER["HTTP_IF_MODIFIED_SINCE"] );
174 $modsinceTime = strtotime( $modsince );
175 $ismodsince = wfTimestamp( TS_MW, $modsinceTime ? $modsinceTime : 1 );
176 wfDebug( "$fname: -- client send If-Modified-Since: " . $modsince . "\n", false );
177 wfDebug( "$fname: -- we might send Last-Modified : $lastmod\n", false );
178
179 if ( ( $ismodsince >= $timestamp ) && $wgUser->validateCache( $ismodsince ) && $ismodsince >= $wgCacheEpoch ) {
180 ini_set( 'zlib.output_compression', 0 );
181 $this->setResponseCode( "304 Not Modified" );
182 $this->disable();
183 $this->mLastModified = $lastmod;
184
185 wfDebug( "$fname: CACHED client: $ismodsince ; user: $wgUser->mTouched ; page: $timestamp ; site $wgCacheEpoch\n", false );
186
187 return true;
188 } else {
189 wfDebug( "$fname: READY client: $ismodsince ; user: $wgUser->mTouched ; page: $timestamp ; site $wgCacheEpoch\n", false );
190 $this->mLastModified = $lastmod;
191 }
192 } else {
193 wfDebug( "$fname: client did not send If-Modified-Since header\n", false );
194 $this->mLastModified = $lastmod;
195 }
196 }
197
198 function loadFromMemcached( $mckey, $touched ) {
199 global $wgMemc;
200
201 if ( !$touched ) {
202 return false;
203 }
204
205 $mcvalue = $wgMemc->get( $mckey );
206 if ( $mcvalue ) {
207 # Check to see if the value has been invalidated
208 if ( $touched <= $mcvalue['timestamp'] ) {
209 wfDebug( "Got $mckey from cache\n" );
210 $this->mText = $mcvalue['value'];
211
212 return true;
213 } else {
214 wfDebug( "$mckey has expired\n" );
215 }
216 }
217
218 return false;
219 }
220
221 function storeInMemcached( $mckey, $expiry = 86400 ) {
222 global $wgMemc;
223
224 $wgMemc->set( $mckey,
225 array(
226 'timestamp' => wfTimestampNow(),
227 'value' => $this->mText
228 ), $expiry
229 );
230
231 return true;
232 }
233 }